package com.woniu.order.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.controller.form.CheckChangeByStationIdForm;
import com.woniu.model.Result;
import com.woniu.order.form.AddEmployeePassForm;
import com.woniu.order.form.AddTransferForm;
import com.woniu.order.model.ChangeDetails;
import com.woniu.order.param.AddEmployeePassParam;
import com.woniu.order.param.AddTransferParam;
import com.woniu.order.param.CheckChangeByStationIdParam;
import com.woniu.order.service.ChangeDetailsService;
import com.woniu.order.util.RedisUtil;
import com.woniu.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@RestController
@RequestMapping("/changeDetails")
@Api(tags = "转单业务")
public class ChangeDetailsController {

    @Resource
    private ChangeDetailsService changeDetailsService;

    @Resource
    private RedisUtil redisUtil;

    /**
     * 员工申请转单
     * @param form
     * @param token
     * @return
     */
    @ApiOperation("员工申请转单")
    @PostMapping("/employeeTransfers")
    public Result<ChangeDetails> employeeTransfers(@RequestBody AddTransferForm form, @RequestHeader("token")String token) throws Exception {
        String refreshToken = redisUtil.get(token);

        Integer userId = Integer.parseInt(JwtUtil.parseToken(refreshToken,JwtUtil.employSecret).get("accountId").toString());

        ChangeDetails changeDetails = changeDetailsService.emplyeeTransfers(userId,BeanUtil.toBean(form, AddTransferParam.class));
        return Result.newInstance(ChangeDetails.class).setData(changeDetails).setMessage("申请成功");
    }

    /**
     * 站长通过转单申请
     * @param form 转单id 目标员工 备注
     * @param token
     * @return
     */
    @ApiOperation("站长通过转单申请")
    @PostMapping("/employeePass")
    public Result<ChangeDetails> employeePass(@RequestBody AddEmployeePassForm form,@RequestHeader("token") String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        Integer userId = Integer.parseInt(JwtUtil.parseToken(refreshToken,JwtUtil.stationBossSecret).get("accountId").toString());

        ChangeDetails changeDetails = changeDetailsService.employeePass(userId,BeanUtil.toBean(form, AddEmployeePassParam.class));
        return Result.newInstance(ChangeDetails.class).setData(changeDetails).setMessage("已通过");
    }

    /**
     * 通过站点id查看转单记录
     * @param form 转单id 目标员工 备注
     * @param token
     * @return
     */
    @ApiOperation("通过站点id查看转单记录")
    @PostMapping("/checkChangeByStationId")
    public Result<Page<ChangeDetails>> checkChangeByStationId(@RequestBody CheckChangeByStationIdForm form, @RequestHeader("token") String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        Integer userId = Integer.parseInt(JwtUtil.parseToken(refreshToken,JwtUtil.stationBossSecret).get("accountId").toString());

        Page<ChangeDetails> page = changeDetailsService.checkChangeByStationId(userId,BeanUtil.toBean(form, CheckChangeByStationIdParam.class));
        return Result.newPageInstance(ChangeDetails.class).setData(page).setMessage("通过站点id查看转单记录成功");
    }

    /**
     * 通过站点id查看转单记录
     * @param form 转单id 目标员工 备注
     * @param token
     * @return
     */
    @ApiOperation("通过站点id查看未处理转单记录")
    @PostMapping("/checkUnHandelChangeByStationId")
    public Result<Page<ChangeDetails>> checkUnHandelChangeByStationId(@RequestBody CheckChangeByStationIdForm form, @RequestHeader("token") String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        Integer userId = Integer.parseInt(JwtUtil.parseToken(refreshToken,JwtUtil.stationBossSecret).get("accountId").toString());

        Page<ChangeDetails> page = changeDetailsService.checkUnHandelChangeByStationId(userId,BeanUtil.toBean(form, CheckChangeByStationIdParam.class));
        return Result.newPageInstance(ChangeDetails.class).setData(page).setMessage("通过站点id查看未处理转单记录成功");
    }

}

